package code.c01_introduction.homework;

/**
 * @author thinkpad - 83start
 * @version v1.0
 * @create 2022/9/9 22:20
 * @package code.c01_introduction.homework
 * @description 求 1 + (1 + 2) + (1 + 2 + 3) + …… + (1 + 2 + …… + n) 的和
 */

public class AdditiveSummation {
    public static void main(String[] args) {
        int n = 100;

        // 测试方法1
        int sum1 = test1(n);
        System.out.println(sum1);

        // 测试方法2
        int sum2 = test2(n);
        System.out.println(sum2);

        // 测试方法3
        int sum3 = test3(n);
        System.out.println(sum3);
    }

    public static int test1(int n) {
        int sum = 0;
        for (int j = 1; j <= n; j++) {
            for (int i = 1; i <= j; i++) {
                sum += i;
            }
        }

        return sum;
    }

    public static int test2(int n) {
        int sum = 0;
        for (int j = 1; j <= n; j++) {
            sum += (j * (j + 1) / 2);
        }

        return sum;
    }

    public static int test3(int n) {
        return (n * (n +1) * (n + 2))/6;
    }

}
